﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vote;

namespace Infornamation
{
    class Program
    {
        static void Main(string[] args)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            List<Account> list1 = new List<Account>();
            AccountInitialize(111111, "111111", "胡岚", list1);
            AccountInitialize(222222, "222222", "张三", list1);
            AccountInitialize(333333, "333333", "李四", list1);
            List<Question> list2 = new List<Question>();
            List<Choices> list3 = new List<Choices>();
            List<Conditional> list4 = new List<Conditional>();
            QusetionInitialize("你最喜欢的男明星是谁？", list2);
            ChoicesInitialize("黄渤1", list3, list2);
            ChoicesInitialize("黄渤2", list3, list2);
            ChoicesInitialize("黄渤3", list3, list2);
            ChoicesInitialize("黄渤4", list3, list2);
            ChoicesInitialize("黄渤5", list3, list2);
            ChoicesInitialize("黄渤6", list3, list2);
            QusetionInitialize("你最喜欢的女明星是谁？", list2);
            ChoicesInitialize("孙俪", list3, list2);
            ChoicesInitialize("张碧晨", list3, list2);
            ChoicesInitialize("高圆圆", list3, list2);
            ChoicesInitialize("张意涵", list3, list2);
            Console.WriteLine("*****************************************");
            Console.WriteLine("**************请先登陆你的Id*************");
            Account User =Landing(list1);
            if (User == null)
            {
                Console.WriteLine("你的三次机会全部用完，请重新打开系统！");
            }
            else
            {
                int a = Println();
                NewMethod(a, User, list2, list3, list4);
                Boolean T = true;
                string ch;
                while (T)
                {
                    Console.WriteLine("是否想继续操作？(Y/N)");
                    ch = Console.ReadLine();
                    if (ch.Equals("Y"))
                    {
                        Console.WriteLine("请按照序号选择你想进行的功能！");
                        int answer = Convert.ToInt32(Console.ReadLine());
                        NewMethod(answer, User, list2, list3, list4);
                    }
                    else
                        T = false;

                }
            }

        }

        public static void NewMethod(int a,Account User, List<Question> list2, List<Choices> list3, List<Conditional> list4)
        {
            switch (a)
            {
                case 1:
                    FunctionOne(User, list2, list3, list4);
                    break;
                case 2:
                    FunctionTwo(list2, list3, list4);
                    break;
                case 3:
                    FunctionThree(list2);
                    break;
                case 4:
                    Console.WriteLine("下次见！！");
                    System.Environment.Exit(0);
                    break;
                default:
                    System.Environment.Exit(0);
                    break;
            }
        }

        public static void AccountInitialize(int number, string password, string name, List<Account> list)
        {

            Account account = new Account(number, password, name);
            list.Add(account);
            account.Id = list.Count;       //用户ID从一开始逐个递增；

        }
        public static void QusetionInitialize(string content, List<Question> list)
        {

            Question question = new Question(content);
            list.Add(question);
            question.Id = list.Count;       //问题ID从一开始逐个递增；

        }
        public static void ChoicesInitialize(string content, List<Choices> list, List<Question> list1)
        {

            Choices choices = new Choices(content);
            list.Add(choices);
            choices.Id_Question = list1.Count;          //选项的Id与问题的Id设为一样；
            choices.Id = list.Count + 100;              //单个选项的Id从101开始；
            

        }
        public static int Println ()
        { 
            
            Console.WriteLine("*****************************************");
            Console.WriteLine("*******请按照相应功能选择你的操作********");
            Console.WriteLine(" 用户登陆");
            Console.WriteLine("1、参与投票");
            Console.WriteLine("2、查看投票统计");
            Console.WriteLine("3、问题过期检测");
            Console.WriteLine("4、退出");

            int index = Convert.ToInt32(Console.ReadLine());
            return index;
        } 
        public static Account Landing(List<Account> list)
        {
            Boolean T = true;
            int num = 6;
            Account User=null;
            int id;
            Console.WriteLine("请输入你的Id");
             while(T)
            {
                id = Convert.ToInt32(Console.ReadLine());
                User = list.FirstOrDefault(p => p.Id == id);
                if(User==null)
                {
                      Console.WriteLine("你输入的ID有误，请重新输入！");
                 }
                else
                {
                    while(num>=4)
                    {
                        Console.WriteLine("请输入你的密码！");
                        string password = Console.ReadLine();
                        if (User.Password.Equals(password))
                        {
                            Console.WriteLine("登入成功,欢迎你,账号为{0}的{1}", User.Number, User.Name);
                            T = false;
                            return User;
                        }
                        else
                        {
                            Console.WriteLine("密码有错误，请重新输入（只有三次机会）！");
                            num--;
                        }        
                    }
                    Console.WriteLine("三次机会用完，系统自动退出！");
                  
                }
            }
            return User;    
        }
        public static void FunctionOne(Account User,List<Question> list2, List<Choices> list3 , List<Conditional> list4)
        {
            int a;
            Choices answer=new Choices();
            if (list4.Count != 0)
            {
                var B = list4.Where(p => p.Account == User).Where(p => p.Selection == null);  //查找此用户没有回答的问题；
                if (B.Count() == 0)
                {
                    Console.WriteLine("每个用户针对每个题目只有一次机会，您已经答题完毕");
                }
                else
                {
                    for(int i=0;i<B.Count();i++)
                    {
                        Conditional b = B.ElementAt(i);
                        Question question = list2.FirstOrDefault(p => p.Id == b.num);
                        Console.WriteLine(question.Content);
                        foreach (Choices choices in list3)
                        {
                            Console.WriteLine("{0}:{1}", choices.Id, choices.Content);
                        }
                        Console.WriteLine("0:不参与此次投票");
                        Console.WriteLine("*******请输入你的选择*********");
                        a = Convert.ToInt32(Console.ReadLine());
                        answer = list3.FirstOrDefault(p => p.Id == a);
                        Conditional conditional = new Conditional(User, DateTime.Now, answer, question.Id);
                        list4.Remove(b);
                        list4.Add(conditional);

                    }
                }
            }
            else
            {
                foreach(Question question in list2)
                {
                    Console.WriteLine(question.Content);
                    var B = list3.Where(p => p.Id_Question == question.Id);
                    foreach(var b in B)
                    {
                        Console.WriteLine("{0}:{1}", b.Id, b.Content);
                    }
                    Console.WriteLine("0:不参与此次投票");
                    Console.WriteLine("*******请输入你的选择*********");
                    a = Convert.ToInt32(Console.ReadLine());
                    answer = list3.FirstOrDefault(p => p.Id == a);
                    Conditional conditional = new Conditional(User, DateTime.Now, answer,question.Id);
                    list4.Add(conditional);
                }
            }
               
        }
        public static void FunctionTwo( List<Question> list2, List<Choices> list3, List<Conditional> list4)
        {
            foreach(Question question in list2)
            {
                var A = list3.Where(p => p.Id_Question == question.Id);
                foreach (var a in A)
                {
                    var C = list4.Where(p => p.Selection!=null).Where(p=>p.Selection.Id==a.Id);
                    Console.WriteLine("有{0}人在{1}问题中，选择了{2}！",C.Count(),question.Content,a.Content);
                }
            }
        }
        public static void FunctionThree(List<Question> list2)
        {
            foreach (Question question in list2)
            {
                TimeSpan ts = DateTime.Now - question.CreationTime;
                if (ts.Hours < 24)
                    Console.WriteLine("问题：{0} 没有过期！", question.Content);
                else
                    Console.WriteLine("问题：{0} 过期辣！", question.Content);
            }
        }
     }

}

